//kskrzypski
#include <GLES/gl.h>
#include <ARToolKitPlus/ar.h>
#include <ARToolKitPlus/TrackerSingleMarker.h>
#include <ARToolKitPlus/TrackerMultiMarker.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>

#define   AR_PATT_NUM_MAX      1 
#define   AR_PATT_SIZE_X       16 
#define   AR_PATT_SIZE_Y       16 
#define   AR_PATT_SAMPLE_NUM   64

#define   USE_TEMPLATE_MARKERS
#define   USE_TRACKER_MULTI

using ARToolKitPlus::TrackerSingleMarker;
using ARToolKitPlus::TrackerMultiMarker;

const float box[] =  {
	// FRONT
	-100.0f, -100.0f,  100.0f,
	 100.0f, -100.0f,  100.0f,
	-100.0f,  100.0f,  100.0f,
	 100.0f,  100.0f,  100.0f,
	// BACK
	-100.0f, -100.0f, -100.0f,
	-100.0f,  100.0f, -100.0f,
	 100.0f, -100.0f, -100.0f,
	 100.0f,  100.0f, -100.0f,
	// LEFT
	-100.0f, -100.0f,  100.0f,
	-100.0f,  100.0f,  100.0f,
	-100.0f, -100.0f, -100.0f,
	-100.0f,  100.0f, -100.0f,
	// RIGHT
	 100.0f, -100.0f, -100.0f,
	 100.0f,  100.0f, -100.0f,
	 100.0f, -100.0f,  100.0f,
	 100.0f,  100.0f,  100.0f,
	// TOP
	 -100.0f,  100.0f,  100.0f,
	 100.0f,  100.0f,  100.0f,
	 -100.0f,  100.0f, -100.0f,
	 100.0f,  100.0f, -100.0f,
	// BOTTOM
	-100.0f, -100.0f,  100.0f,
	-100.0f, -100.0f, -100.0f,
	 100.0f, -100.0f,  100.0f,
	 100.0f, -100.0f, -100.0f,
};
const float normals[] =  {
	// FRONT
	0.0f, 0.0f,  1.0f,
	0.0f, 0.0f,  1.0f,
	0.0f, 0.0f,  1.0f,
	0.0f, 0.0f,  1.0f,
	// BACK
	0.0f, 0.0f,  -1.0f,
	0.0f, 0.0f,  -1.0f,
	0.0f, 0.0f,  -1.0f,
	0.0f, 0.0f,  -1.0f,
	// LEFT
	-1.0f, 0.0f,  0.0f,
	-1.0f, 0.0f,  0.0f,
	-1.0f, 0.0f,  0.0f,
	-1.0f, 0.0f,  0.0f,
	// RIGHT
	1.0f, 0.0f,  0.0f,
	1.0f, 0.0f,  0.0f,
	1.0f, 0.0f,  0.0f,
	1.0f, 0.0f,  0.0f,
	// TOP
	0.0f, 1.0f,  0.0f,
	0.0f, 1.0f,  0.0f,
	0.0f, 1.0f,  0.0f,
	0.0f, 1.0f,  0.0f,
	// BOTTOM
	0.0f, -1.0f,  0.0f,
	0.0f, -1.0f,  0.0f,
	0.0f, -1.0f,  0.0f,
	0.0f, -1.0f,  0.0f,
};
//Lighting variables
const static GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
const static GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
const static GLfloat   mat_flash_shiny[] = {50.0};
const static GLfloat   light_position[]  = {100.0,-200.0,200.0,0.0};
const static GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
const static GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};

const int width1 = 4, height1 = 3;//that is only for initialization
char *pattern = (char *)"/sdcard/patt.hiro";//template marker pattern
static int markersCount = 0;
int initialized = 0;
int patternID = 0;
long getSetting;
const int TEMPLATE = 0;
const int NOT_TEMPLATE = 1;
static int marker_mode = NOT_TEMPLATE;
const int TRACKER_SINGLE = 0;
const int TRACKER_MULTI = 1;
static int tracker_mode = TRACKER_SINGLE;
struct timespec startMarkerDetect, endMarkerDetect;
struct timespec startAll, endAll;
uint64_t timeElapsedMarkerDetection;
uint64_t timeElapseAll;

static long BIT_MODE_MARKER_TYPE_TEMPLATE = 1<<13;
static long BIT_MODE_MARKER_TYPE_SIMPLE = 1<<14;
static long BIT_MODE_MARKER_TYPE_BCH = 1<<15;
//pose estimator
static long BIT_MODE_POSE_ESTIMATOR_ORIGINAL = 1<<16;
static long BIT_MODE_POSE_ESTIMATOR_ORIGINAL_CONT = 1<<17;
static long BIT_MODE_POSE_ESTIMATOR_RPP = 1<<18;
//tracker type
static long BIT_MODE_TRACKER_BASE_TYPE_SINGLE = 1<<19;
static long BIT_MODE_TRACKER_BASE_TYPE_MULTI = 1<<20;	
//hull tracking mode
static long BIT_MODE_HULL_OFF = 1<<21;
static long BIT_MODE_HULL_FOUR = 1<<22;
static long BIT_MODE_HULL_FULL = 1<<23;

//for tests
static const ARFloat* para;

static void argConvGlpara(const ARFloat para[3][4], GLfloat gl_para[16] );
void gray8toRGB32(jbyte* src, jint* dst, int len); 
int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p);
